#ifndef LIBMAD_BIT_H
#define LIBMAD_BIT_H

//#include "iphoneporting.h"

#define CHAR_BIT  8

struct mad_bitptr {
	unsigned char const *byte;
	unsigned short cache;
	unsigned short left;
};

struct bits_code
{
	unsigned short wval_code_bits; 
	unsigned char bval_code_len;
	char cval_data_type;	// 'C'-const, 'D'-DWORD, 'I'-INT32, 'X'-XInt32, 'H'-hundred DWORD, 'h'-hundred INT
	unsigned char bval_data_len;
	unsigned long dwval_data_bias;
} ;

typedef struct XINT32_s {
	
	int baseVal:29;
	unsigned int mulVal:3;
	
} XINT32;


#if defined(__cplusplus)
extern "C" {
#endif
	
	
	void XINT32_INIT_INT64(XINT32 *,__int64 i64val);
	void XINT32_INIT_INT32(XINT32 *,int i32val);
	__int64 XINT32_VALINT64(XINT32 *);
	int XINT32_VALINT32(XINT32 *);
	
	/* End don't mangle the function name if compile under C++ */
#if defined (__cplusplus)
}
#endif


void mad_bit_init(struct mad_bitptr *, unsigned char const *);

# define mad_bit_finish(bitptr)		/* nothing */

unsigned int mad_bit_length(struct mad_bitptr const *,
							struct mad_bitptr const *);

# define mad_bit_bitsleft(bitptr)  ((bitptr)->left)

unsigned char const *mad_bit_nextbyte(struct mad_bitptr const *);

void mad_bit_skip(struct mad_bitptr *, unsigned int);
unsigned long mad_bit_read(struct mad_bitptr *, unsigned int);
void mad_bit_write(struct mad_bitptr *, unsigned int, unsigned long);

unsigned short mad_bit_crc(struct mad_bitptr, unsigned int, unsigned short);

# endif
